﻿using System;
using System.Collections.Generic;
using System.Text;
using Ex04.Menus.Delegates;

namespace Ex04.Menus.Test
{
    public class DelegatesMenuUser
    {
        private MainMenu m_MainMenu;

        public DelegatesMenuUser()
        {
            m_MainMenu = new MainMenu("Delegates Main Menu");

            SubMenu firstItem = new SubMenu("First - Should Open A SubMenu.");
            MenuAction secondItem = new MenuAction("Second - Should Invoke An Action.");
            SubMenu thirdItem = new SubMenu("Third - Should Open A SubMenu.");
            MenuAction fourthItem = new MenuAction("Fourth - Should Invoke An Action.");

            m_MainMenu.AddMenuItem(firstItem);
            m_MainMenu.AddMenuItem(secondItem);
            m_MainMenu.AddMenuItem(thirdItem);
            m_MainMenu.AddMenuItem(fourthItem);

            secondItem.ItemChosen += new EventHandler(menuItem_ItemChosen);
            fourthItem.ItemChosen += new EventHandler(menuItem_ItemChosen);

            MenuAction firstFirstItem = new MenuAction("First First - Should Invoke An Action.");
            MenuAction firstSecondItem = new MenuAction("First Second - Should Invoke An Action.");
            MenuAction firstThirdItem = new MenuAction("First Third - Should Invoke An Action.");

            firstItem.AddMenuItem(firstFirstItem);
            firstItem.AddMenuItem(firstSecondItem);
            firstItem.AddMenuItem(firstThirdItem);

            firstFirstItem.ItemChosen += new EventHandler(menuItem_ItemChosen);
            firstSecondItem.ItemChosen += new EventHandler(menuItem_ItemChosen);
            firstThirdItem.ItemChosen += new EventHandler(menuItem_ItemChosen);

            SubMenu ThirdFirstItem = new SubMenu("Third First - Should Open A SubMenu.");
            MenuAction ThirdSecondItem = new MenuAction("Third Second - Should Invoke An Action.");
            
            thirdItem.AddMenuItem(ThirdFirstItem);
            thirdItem.AddMenuItem(ThirdSecondItem);

            ThirdSecondItem.ItemChosen += new EventHandler(menuItem_ItemChosen);

            MenuAction ThirdFirstFirstItem = new MenuAction("Third First First - Should Invoke An Action.");
            MenuAction ThirdFirstSecondItem = new MenuAction("Third First Second - Should Invoke An Action.");

            ThirdFirstItem.AddMenuItem(ThirdFirstFirstItem);
            ThirdFirstItem.AddMenuItem(ThirdFirstSecondItem);

            ThirdFirstFirstItem.ItemChosen += new EventHandler(menuItem_ItemChosen);
            ThirdFirstSecondItem.ItemChosen += new EventHandler(menuItem_ItemChosen);
        }

        private void menuItem_ItemChosen(object sender, EventArgs e)
        {
            MenuAction menuItem = sender as MenuAction;
            string messageToUser = string.Format("You chose: {0}", menuItem.DisplayText);
            Console.WriteLine(messageToUser);
            Console.ReadLine();
        }

        public void ShowMenu()
        {
            m_MainMenu.Show();
        }
    }
}